package com.example.zhinongbao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.zhinongbao.R;
import com.example.zhinongbao.model.SellerFunction;

import java.util.List;

public class SellerFunctionAdapter extends BaseAdapter {
    private List<SellerFunction> functions;
    private LayoutInflater inflater;

    public SellerFunctionAdapter(Context context, List<SellerFunction> functions) {
        this.functions = functions;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() { return functions.size(); }

    @Override
    public Object getItem(int position) { return functions.get(position); }

    @Override
    public long getItemId(int position) { return position; }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_seller_function, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        SellerFunction function = functions.get(position);
        holder.ivIcon.setImageResource(function.getIconResId());
        holder.tvTitle.setText(function.getTitle());

        return convertView;
    }

    static class ViewHolder {
        ImageView ivIcon;
        TextView tvTitle;

        ViewHolder(View view) {
            ivIcon = view.findViewById(R.id.iv_icon);
            tvTitle = view.findViewById(R.id.tv_title);
        }
    }
}